const errorTypes = require("../constants/error-types")

const errorHandler = (error, ctx) => {
    let message,status;

    switch(error.message) {
        case errorTypes.USERNAME_OR_PASSWORD_IS_NULL:
            message = "用户名或密码为空"
            status = 400
            break;
        case errorTypes.USERNAME_ALREADY_EXISTS:
            message = "用户名以存在"
            status = 409
            break;
        case errorTypes.USERNAME_NOT_EXISTS:
            message = "用户不存在"
            status = 400
            break;
        case errorTypes.PASSWORD_IS_MISTAKE:
            message = "密码错误"
            status = 400
            break;
        case errorTypes.UNAUTHORIZATION:
            message = "无效的token"
            status = 401
            break;
        case errorTypes.UNPERMISSION:
            message = "权限不足"
            status = 403
            break;
        default:
            message = "NOT FOUND"
            status = 404
    }
    
    ctx.status = status;
    ctx.body = message
}

module.exports = errorHandler